﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using clEntidad.Ex;

namespace clEntidad
{
    public class Seguridad
    {
        SymmetricAlgorithm sa = null;
        String mStr_IV = "Mi mamita me mim";
        public String  getMD5(String pStr_Valor){
            MD5CryptoServiceProvider vMd5 = new MD5CryptoServiceProvider();
            UnicodeEncoding Ue =  new UnicodeEncoding();
            byte [] aBytMD5;
            byte [] aByt;
            StringBuilder stb = new StringBuilder();
            aByt = Encoding.Default.GetBytes(pStr_Valor);
            aBytMD5 = vMd5.ComputeHash(aByt);
            for (int i=0;i < aBytMD5.Length;i++)
            {
                stb.Append(aBytMD5[i].ToString("X2"));
            }
            return stb.ToString();
        }

        public Seguridad() {
            sa = new RijndaelManaged();
        }
        public byte[] cifradoSimetricoMS(String pStr_Mensaje, String pStr_Key)
        {
            MemoryStream msEncrypt = null;
            try
            {
                sa.Key = new ASCIIEncoding().GetBytes(pStr_Key);
                sa.IV = new ASCIIEncoding().GetBytes(mStr_IV);

                ICryptoTransform encryptor = sa.CreateEncryptor(sa.Key, sa.IV);
                // Create the streams used for encryption.
                msEncrypt = new MemoryStream();
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //'Write all data to the stream.
                        swEncrypt.Write(pStr_Mensaje);
                    }
                }
            }
            catch (Exception)
            {
                // Clear the RijndaelManaged object.
                if (sa != null)
                    sa.Clear();

            }
            return msEncrypt.ToArray();
        }
        public String DescifradoSimetricoMS(byte[] cipherText, String pStr_Key)
        {
            try
            {
                sa.Key = new ASCIIEncoding().GetBytes(pStr_Key);
                sa.IV = new ASCIIEncoding().GetBytes(mStr_IV);
                //' Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = sa.CreateDecryptor(sa.Key, sa.IV);
                String plaintext;
                //' Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            //' Read the decrypted bytes from the decrypting stream
                            //' and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
                return plaintext;
            }
            catch (Exception)
            {
                throw new BUSException("Clave Incorrecta");
            }
            finally
            {
                //' Clear the RijndaelManaged object.
                if (sa != null)
                    sa.Clear();
            }
        }

    }
}
